package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {


    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomAttrValueService  roomAttrValueService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        //先判断是否为空 不为空就先删除将他清空,然后再进行插入操作    这就是新增和修改操作的方法 有就删除,没有就新增
        if (roomSubmitVo.getId() != null){

            //获取传过来的id的值,他携带过来的参数里面会有图片的id和类型
            Long roomId = roomSubmitVo.getId();
            //1.删除图片
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,roomId)
                            .eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);

            //2.删除房间配套信息
            LambdaQueryWrapper<RoomFacility> roomFacility = new LambdaQueryWrapper<>();
            roomFacility.eq(RoomFacility::getRoomId,roomId);
            roomFacilityService.remove(roomFacility);

            //3.删除房间标签信息
            LambdaQueryWrapper<RoomLabel> roomLabel = new LambdaQueryWrapper<>();
            roomLabel.eq(RoomLabel::getRoomId,roomId);
            roomLabelService.remove(roomLabel);

            //4.删除房间属性信息
            LambdaQueryWrapper<RoomAttrValue> roomAttrValue = new LambdaQueryWrapper<>();
            roomAttrValue.eq(RoomAttrValue::getRoomId,roomId);
            roomAttrValueService.remove(roomAttrValue);

            //5.删除房间支付方式信息
            LambdaQueryWrapper<RoomPaymentType> roomPaymentType = new LambdaQueryWrapper<>();
            roomPaymentType.eq(RoomPaymentType::getRoomId,roomId);
            roomPaymentTypeService.remove(roomPaymentType);

            //6.删除房间租期信息
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTerm = new LambdaQueryWrapper<>();
            roomLeaseTerm.eq(RoomLeaseTerm::getRoomId,roomId);
            roomLeaseTermService.remove(roomLeaseTerm);
        }


        //先应该新增入库,回显id
        this.saveOrUpdate(roomSubmitVo);
        Long roomId = roomSubmitVo.getId();
        //1.新增图片信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> list = new ArrayList(graphVoList.size());
        if (!CollectionUtils.isEmpty(graphVoList)){
            //将用户的数据vo转化为可以入库的对象
            graphVoList.forEach(graphVo -> {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(roomId);
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                list.add(graphInfo);
            });
            graphInfoService.saveBatch(list);
        }
        //2.新增配套信息
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        List<RoomFacility> roomFacilityList = new ArrayList<>(facilityInfoIds.size());
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            facilityInfoIds.forEach(facilityId ->{
                RoomFacility roomFacility = RoomFacility.builder()
                        .facilityId(facilityId)
                        .roomId(roomId)
                        .build();
                roomFacilityList.add(roomFacility);
            });
            roomFacilityService.saveBatch(roomFacilityList);
        }

        //3.新增房间标签信息
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        List<RoomLabel> roomLabelList = new ArrayList<>(labelInfoIds.size());
        if (!CollectionUtils.isEmpty(labelInfoIds)){
            labelInfoIds.forEach(labelInfoId ->{
                RoomLabel roomLabel = RoomLabel.builder()
                        .labelId(labelInfoId)
                        .roomId(roomId)
                        .build();
                roomLabelList.add(roomLabel);
            });
            roomLabelService.saveBatch(roomLabelList);
        }
        //4.新增房间属性信息
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        List<RoomAttrValue> roomAttrValueList = new ArrayList<>(attrValueIds.size());
        if (!CollectionUtils.isEmpty(attrValueIds)){
            attrValueIds.forEach(attrValueId ->{
                RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                        .attrValueId(attrValueId)
                        .roomId(roomId)
                        .build();
                roomAttrValueList.add(roomAttrValue);
            });
            roomAttrValueService.saveBatch(roomAttrValueList);
        }
        //5.房间支付信息
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>(paymentTypeIds.size());
        if (!CollectionUtils.isEmpty(paymentTypeIds)){
            paymentTypeIds.forEach(paymentTypeId ->{
                RoomPaymentType roomPaymentType = RoomPaymentType.builder()
                        .paymentTypeId(paymentTypeId)
                        .roomId(roomId)
                        .build();
                roomPaymentTypeList.add(roomPaymentType);
            });
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }
        //6.房间租期信息
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>(leaseTermIds.size());
        if (!CollectionUtils.isEmpty(leaseTermIds)){
            leaseTermIds.forEach(leaseTermId ->{
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                        .leaseTermId(leaseTermId)
                        .roomId(roomId)
                        .build();
                roomLeaseTermList.add(roomLeaseTerm);
            });
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }

    }

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
       return roomInfoMapper.pageRoomItemByQuery(page,queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        return roomInfoMapper.getRoomDetailById(id);
    }

    @Override
    public void removeRoomById(Long id) {
        //1.删除图片

        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id)
                        .eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        //2.删除房间配套
        LambdaQueryWrapper<RoomFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(facilityQueryWrapper);
        //3.删除房间标签
        LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(labelQueryWrapper);
        //4.删除房间属性
        LambdaQueryWrapper<RoomAttrValue> attrQueryWrapper = new LambdaQueryWrapper<>();
        attrQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(attrQueryWrapper);
        //5.删除房间支付方式
        LambdaQueryWrapper<RoomPaymentType> paymentQueryWrapper = new LambdaQueryWrapper<>();
        paymentQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(paymentQueryWrapper);
        //6.删除房间租期关系
        LambdaQueryWrapper<RoomLeaseTerm> termQueryWrapper = new LambdaQueryWrapper<>();
        termQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(termQueryWrapper);

        //7.删除自己
        this.removeById(id);
    }
}




